﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace dotNetFxInstaller
{
    
    public partial class Form1 : Form
    {
        Process process = new Process();
        ProcessStartInfo processStartInfo;

        List<string> driveList, labelList;
        public Form1()
        {
            InitializeComponent();
        }


        private void installBtn_Click(object sender, EventArgs e)
        {
            processStartInfo = new ProcessStartInfo();
            processStartInfo.UseShellExecute = true;
            //processStartInfo.WorkingDirectory = Path.GetPathRoot(Environment.SystemDirectory) + @"/Windows/System32";
            processStartInfo.FileName = "cmd.exe";
            processStartInfo.Verb = "runas";
            if (radioButton1.Checked)
            {
                //Dism  /online /enable-feature /featurename:NetFx3 /All /Source:J:\sources\sxs /LimitAccess


                try
                {
                    //  processStartInfo.FileName = "cmd.exe";
                    Console.WriteLine("Command: " + "Dism  /online /enable-feature /featurename:NetFx3 /All /Source:" + comboBox1.Text + @"sources\sxs /LimitAccess");
                    //  Process.Start("Dism  /online /enable-feature /featurename:NetFx3 /All /Source:" + comboBox1.Text + @"\sources\sxs /LimitAccess");
                    processStartInfo.Arguments = "/k Dism  /online /enable-feature /featurename:NetFx3 /All /Source:" + comboBox1.SelectedItem + @"sources\sxs /LimitAccess";
                    Process.Start(processStartInfo);

                }

                catch (Win32Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }
            }
            else if (radioButton2.Checked)
            {
                try
                {
                    string command = "/k Dism  /online /enable-feature /featurename:NetFx3 /All /Source:" + path + @"\sxs /LimitAccess";
                    Console.WriteLine("Command: " + command);
                    processStartInfo.Arguments =command;
                    Process.Start(processStartInfo);
                }
                catch (Win32Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }
            }

        }

        private void ListAvailableDrives()
        {
            driveList = new List<string>();
            labelList = new List<string>();
            DriveInfo[] driveInfo = DriveInfo.GetDrives();
            foreach (DriveInfo info in driveInfo)
            {
                if (info.DriveType == DriveType.CDRom || info.DriveType == DriveType.Removable)
                {
                    if (info.IsReady)
                    {
                        driveList.Add(info.RootDirectory.FullName);
                        labelList.Add(info.VolumeLabel);
                    }
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ListAvailableDrives();
          //  comboBox1.Text = comboBox1.SelectedItem + labelList[comboBox1.SelectedIndex];
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
          
        }

        private void comboBox1_DropDown(object sender, EventArgs e)
        {
            ListAvailableDrives();
            bindingSource1.DataSource = driveList;
            comboBox1.DataSource = bindingSource1.DataSource;
            comboBox1.DisplayMember = "Name";
            //   comboBox1.ValueMember = "Name";
            if (labelList != null && labelList.Count > 0) 
            {
                comboBox1.Text = comboBox1.SelectedItem + " [" + labelList[comboBox1.SelectedIndex] + "]";
            }

        }
        string path = "";
        private void button2_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            textBox1.Text =path= folderBrowserDialog1.SelectedPath;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if(radioButton2.Checked)
            {
                textBox1.Enabled = true;
                button2.Enabled = true;
                comboBox1.Enabled = false;
            }
            else
            {
                textBox1.Enabled = false;
                button2.Enabled = false;
                comboBox1.Enabled = true;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

            if (radioButton1.Checked)
            {
                textBox1.Enabled = false;
                button2.Enabled = false;
                comboBox1.Enabled = true;
            }
            else
            {
                textBox1.Enabled = true;
                button2.Enabled = true;
                comboBox1.Enabled = false;
            }
        }
    }
}
